package com.lagou.edu.minicat;

import com.lagou.edu.minicat.core.Host;
import lombok.Data;

import java.io.*;

/**
 * @author gaoguangyang
 */
@Data
public class Response {
    private Host host;
    private OutputStream outputStream;

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void write(String content) throws IOException {
        this.outputStream.write(content.getBytes());
    }

    public void outputHtml(String path) throws IOException {
        String absolutePath = StaticResourceUtil.getAbsolutePath(path);

        File file = new File(absolutePath);
        if (file.exists() && file.isFile()) {
            InputStream fis = new FileInputStream(file);
            StaticResourceUtil.outputResource(fis, outputStream);
        } else {
            outputStream.write(ProtocolHttpUtil.http404header().getBytes());
        }

    }

    public void outputHtml1(String path) throws IOException {
        String absolutePath = host.getAppBase() + "/" + path;
        File file = new File(absolutePath);
        if (file.exists() && file.isFile()) {
            InputStream fis = new FileInputStream(file);
            StaticResourceUtil.outputResource(fis, outputStream);
        } else {
            outputStream.write(ProtocolHttpUtil.http404header().getBytes());
        }
    }
}
